<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .container {
      width: 500px;
      height: 500px;
      border: 1px solid #ddd;
      display: flex;
      justify-content: space-between;
      flex-direction: column;
    }

    .footer {
      width: 100%;
      height: 100px;
      border-top: 1px solid #ddd;
      padding-left: 10px;
      box-sizing: border-box;
    }

    ul {
      list-style: none;
      padding: 0;
    }

    .xm {
      color: green;
      text-align: left;
    }

    .xh {
      color: pink;
      text-align: right;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="container">
      <ul>
        <li v-for="(item) in list" v-bind:class="{xm:item.type ==='xm',xh:item.type==='xh'}">
          {{item.type==='xm'?'小明说：':'小红说：'}}
          {{ item.msg }}</li>
      </ul>

      <div class="footer">
        <child1 v-on:child1-to-parent="appendList"></child1>
        <child2 v-on:child2-to-parent="appendList"></child2>
      </div>
    </div>
  </div>

  <script src="js/vue.js"></script>
  <script>
    Vue.component('child1', {
      data() {
        return {
          inpValue: ''
        }
      },
      template: `
        <div class="child1">
          <input type="text" v-model="inpValue" placeholder="小明说" />
          <button v-on:click="child1Send">发送</button>
        </div>
      `,
      methods: {
        child1Send() {
          if (this.inpValue) {
            this.$emit('child1-to-parent', {
              msg: this.inpValue,
              type: 'xm'
            })

            this.inpValue = ''
          }
        }
      }
    })

    Vue.component('child2', {
      data() {
        return {
          inpValue: ''
        }
      },
      template: `
        <div class="child2">
          <input type="text" v-model="inpValue" placeholder="小红说" />
          <button v-on:click="child2Send">发送</button>
        </div>
      `,
      methods: {
        child2Send() {
          if (this.inpValue) {
            this.$emit('child2-to-parent', {
              msg: this.inpValue,
              type: 'xh'
            })

            this.inpValue = ''
          }
        }
      }
    })

    var myApp = new Vue({
      el: '#app',
      data: {
        list: []
      },
      methods: {
        appendList(data) {
          this.list.push(data)
        }
      }
    })
  </script>
</body>

</html>